package com.tianyang.yukuaifu.module.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.common.utils.CommonUtils;
import com.tianyang.yukuaifu.common.utils.DateCheckListener;
import com.tianyang.yukuaifu.module.dialog.interfaces.IChooseDateListener;
import com.tianyang.yukuaifu.module.roundview.RoundTextView;
import com.tianyang.yukuaifu.utils.DateUtils;
import com.tianyang.yukuaifu.utils.ToastUtil;

import java.util.Calendar;

/**
 * Description：时间选择
 */
public class ChooseDateDialog {

    private RoundTextView startDateRtv, endDateRtv;

    private LinearLayout llStartDate, llEndDate;

    private TextView cancelTv, confirmTv;

    private Dialog aDialog;

    private Context mContext;

    private Calendar mCalendar = Calendar.getInstance();

    private String startDate, endDate;

    private IChooseDateListener mListener;

    public ChooseDateDialog(Context context) {
        this.mContext = context;
        ChooseDateDialog(mContext);
    }

    public ChooseDateDialog(Context context, String startDate, String endDate) {
        this.mContext = context;
        this.startDate = startDate;
        this.endDate = endDate;
        ChooseDateDialog(mContext);
    }

    public void setListener(IChooseDateListener mListener) {
        this.mListener = mListener;
    }

    private void ChooseDateDialog(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_choose_date, null);
        aDialog = new Dialog(context, R.style.bottom_pop_menu);

        startDateRtv = inflate.findViewById(R.id.startDateRtv);
        endDateRtv = inflate.findViewById(R.id.endDateRtv);
        llStartDate = inflate.findViewById(R.id.llStartDate);
        llEndDate = inflate.findViewById(R.id.llEndDate);
        cancelTv = inflate.findViewById(R.id.cancelTv);
        confirmTv = inflate.findViewById(R.id.confirmTv);

        //设置默认时间
        if (TextUtils.isEmpty(startDate)) {
            startDateRtv.setText(CommonUtils.getMonthFirstDay());
        } else {
            startDateRtv.setText(startDate);
        }
        if (TextUtils.isEmpty(endDate)) {
            endDateRtv.setText(DateUtils.formatTime(mCalendar.getTime().getTime(), "yyyy-MM-dd"));
        } else {
            endDateRtv.setText(endDate);
        }
        new DateCheckListener((Activity) mContext).handleRegionCheck(llStartDate, startDateRtv, true);
        new DateCheckListener((Activity) mContext).handleRegionCheck(llEndDate, endDateRtv, true);

        setUiBeforShow();

        aDialog.setContentView(inflate);
//        aDialog.setCanceledOnTouchOutside(false);
//        aDialog.setCancelable(false);
    }

    private void setUiBeforShow() {
        startDateRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startDateRtv.getDelegate().setStrokeColor(ContextCompat.getColor(mContext, R.color.c_4a4a4a));
                endDateRtv.getDelegate().setStrokeColor(ContextCompat.getColor(mContext, R.color.c_c4c4c4));
                llStartDate.setVisibility(View.VISIBLE);
                llEndDate.setVisibility(View.GONE);
            }
        });
        endDateRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startDateRtv.getDelegate().setStrokeColor(ContextCompat.getColor(mContext, R.color.c_c4c4c4));
                endDateRtv.getDelegate().setStrokeColor(ContextCompat.getColor(mContext, R.color.c_4a4a4a));
                llStartDate.setVisibility(View.GONE);
                llEndDate.setVisibility(View.VISIBLE);
            }
        });
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String startDateStr = startDateRtv.getText().toString();
                String endDateStr = endDateRtv.getText().toString();
                if (TextUtils.isEmpty(startDateStr)) {
                    ToastUtil.show("请选择开始时间");
                    return;
                }
                if (TextUtils.isEmpty(endDateStr)) {
                    ToastUtil.show("请选择结束时间");
                    return;
                }
                if (DateUtils.getDifferenceTime(startDateStr, endDateStr) > 30) {
                    ToastUtil.show("查询时间区间不能大于30天");
                    return;
                }
                if (DateUtils.getDifferenceTime(startDateStr, endDateStr) < 0) {
                    ToastUtil.show("查询终止日期必须大于起始日期");
                    return;
                }
                if (mListener != null) {
                    mListener.confirm(startDateStr, endDateStr);
                    dismiss();
                }
            }
        });
    }

    public void dismiss() {
        if (aDialog != null && aDialog.isShowing()) {
            aDialog.dismiss();
        }
    }

    public void show() {
        if (aDialog != null && !aDialog.isShowing()) {
            aDialog.show();
        }
    }
}
